package com.education.myoschinatest.http;

import com.education.myoschinatest.bean.DataBean;
import com.education.myoschinatest.config.UrlConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Url;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.observers.Observers;
import rx.observers.Subscribers;
import rx.schedulers.Schedulers;

public class HttpUtils {

    private static volatile HttpUtils singleton;
    private Retrofit retrofit;
    private IDouyu douyu;

    private HttpUtils() {
        retrofit = createRetrofit();
        douyu = createDouyu();
    }

    public static HttpUtils getInstance() {
        if (singleton == null) {
            synchronized (HttpUtils.class) {
                if (singleton == null) {
                    singleton = new HttpUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * 创建retrofit的实例
     * @return
     */
    private Retrofit createRetrofit() {
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .addCallAdapterFactory
                        (RxJavaCallAdapterFactory
                                .createWithScheduler(
                                        Schedulers.newThread()))
                .build();
    }

    /**
     * 创建gson实例,用于retrofit解析返回的结果
     * @return
     */
    private Gson createGson() {
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();
        return gson;
    }

    /**
     * 创建网络连接接口的实例
     * @return
     */
    public IDouyu createDouyu() {
        IDouyu douyu = retrofit.create(IDouyu.class);
        return douyu;
    }
    public interface HttpCallBack<T>{
        public void onSucess(MyHttpBody<T> body);
        public void onFaile(int erroCode,Throwable throwable);
    }
    /**
     * 网络获取颜值的方法
     *
     * @param params   参数列表 包含两个参数 limit和offset
     * @param callback 访问数据的回调接口
     */
    public void getVerticalData(Map<String, String> params,final HttpCallBack<DataBean> callback) {

    }


}